// pages/editaddress/editaddress.js
const interfaces = require("../../utils/request.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: "",
    phoneNumber: "",
    city: "",
    detailAddress: "",
    region: ['广东省', '广州市', '海珠区'],
    customItem: "全部",
    address: "",
    isChecked: false,
    checked: false,
    id: "",
    addressId: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    const id = options.id
    // console.log(typeof id)
    if(id){
      this.setData({
        addressId: id
      })
      this.getDetailAddress(id)
    }else{
      return
    }
  },
  getDetailAddress(id){
    const self = this
    wx.request({
      url: interfaces.getDetailAction,
      data:{
        id: id
      },
      success(res){
        let data = res.data.data
        let check = data.is_default == 1 ? true : false
        console.log(res)
        self.setData({
          userName: data.name,
          phoneNumber: data.mobile,
          address: data.address,
          detailAddress: data.address_detail,
          checked: check
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  tapUserNname(e){
    this.setData({
      userName: e.detail.value
    })
  },
  tapPhoneNumber(e) {
    this.setData({
      phoneNumber: e.detail.value
    })
  },
  tabDetailAddress(e) {
    this.setData({
      detailAddress: e.detail.value
    })
  },
  /**
   * 多选框
   */
  checkboxChange(e){
    //console.log(e.detail)
    let value = e.detail.value
    if(value.length>0){
      this.setData({
        checked: e.detail.value[0]
      })
    }else{
      this.setData({
        checked: false
      })
    }
  },
  /**
   * 省市区点击事件
   */
  bindRegionChange(e){
    let value = e.detail.value
    this.setData({
      address: value[0] + " " + value[1] + " " + value[2]
    })
    //console.log(this.data.address)
  },
  /**
   * 添加地址
   */
  onSaveAddr(){
    const self = this
    let openId = wx.getStorageSync('openId')
    let userName = this.data.userName
    let mobile = this.data.phoneNumber
    let detailAddress = this.data.detailAddress
    let address = this.data.address
    //console.log(this.data.address)
    if (!userName){
      wx.showToast({
        title: '请输入收货人姓名',
        icon: "none"
      })
      return;
    }
    if (!mobile) {
      wx.showToast({
        title: '请输入手机号',
        icon: "none"
      })
      return;
    }
    if (mobile.length != 11) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: "none"
      })
      return;
    }
    if (!detailAddress) {
      wx.showToast({
        title: '请输入详细地址',
        icon: "none"
      })
      return;
    }
    if (!address) {
      wx.showToast({
        title: '请输入所在地区',
        icon: "none"
      })
      return;
    }
    wx.showLoading({
      title: ''
    });
    wx.request({
      url: interfaces.saveAddress,
      method: "POST",
      data:{
        userName: self.data.userName,
        telNumber: self.data.phoneNumber,
        address: self.data.address,
        detailadress: self.data.detailAddress,
        checked: self.data.checked,
        openId: openId,
        addressId: self.data.addressId
      },
      success(res){
        wx.hideLoading();
        wx.showToast({
          title: "地址添加成功",
          icon: "none",
          success: res=>{
            wx.navigateBack({
              delta: 1
            })
          }
        });
      }
    })
  },
  /**
   * 删除地址
   */
  onDelete(){
    const self = this
    wx.showLoading({
      title: ''
    });
    wx.request({
      url: interfaces.deleteAction,
      data:{
        id: self.data.addressId
      },
      success(res){
        wx.hideLoading();
        wx.showToast({
          title: "成功删除地址",
          icon: "none",
          success: res => {
            wx.navigateBack({
              delta: 1
            })
          }
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})